package appBord

import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONObject

class AppBordAlarmController {

    def index() {
        initData()
        render AppBordAlarm.list() as JSON
    }

    /**
     * 初始化
     * @return
     */
    def initData(){
        if(AppBordAlarm.count()<=0){
            def a = new AppBordAlarm(alarmNo:"CRITICAL",alarmName:"严重告警",alarmColor:"255,0,0",alarmFlash:500)
            a.save(flush: true)
            a = new AppBordAlarm(alarmNo:"MAJOR",alarmName:"主要告警",alarmColor:"255,165,0",alarmFlash:1000)
            a.save(flush: true)
            a = new AppBordAlarm(alarmNo:"MINOR",alarmName:"次要告警",alarmColor:"255,255,0",alarmFlash:2000)
            a.save(flush: true)
            a = new AppBordAlarm(alarmNo:"WARNING",alarmName:"警告告警",alarmColor:"230,230,250",alarmFlash:5000)
            a.save(flush: true)
            a = new AppBordAlarm(alarmNo:"INDETERMINATE",alarmName:"未知告警",alarmColor:"169,169,169",alarmFlash:10000)
            a.save(flush: true)
            a = new AppBordAlarm(alarmNo:"CLEARED",alarmName:"已清除告警",alarmColor:null,alarmFlash:0)
            a.save(flush: true)
        }
    }

    def show(){
        def alamInstance
        if(params?.id){
            alamInstance = AppBordAlarm.get(params?.id)
        }else{
            alamInstance = AppBordAlarm.newInstance()
        }

        render(view: "/appBord/appBordAlarm/update.gsp",model: [alamInstance:alamInstance])
    }

    def update(){

        def resultJson = JSONObject.newInstance()
        resultJson.put("statusCode",200)
        resultJson.put("message","操作成功")
        try {
            def alarmNo = params.alarmNo
            def appBordAlarm = AppBordAlarm.findByAlarmNo(alarmNo)
            if(!appBordAlarm){
                appBordAlarm = AppBordAlarm.newInstance()
            }

            def alarmName = params.alarmName
            def alarmColor = params.alarmColor
            def alarmFlash = params.alarmFlash?:0
            alarmFlash = Integer.parseInt(alarmFlash)

            appBordAlarm.alarmNo = alarmNo
            appBordAlarm.alarmName = alarmName
            appBordAlarm.alarmColor = alarmColor
            appBordAlarm.alarmFlash = alarmFlash
            appBordAlarm.save(flush: true)
        }catch (Exception e){
            resultJson.put("statusCode",-200)
            resultJson.put("message","Error:${e.getMessage()}")
            e.printStackTrace()
        }finally{
            render resultJson.toString()
        }
    }

    def delete(){
        def resultJson = JSONObject.newInstance()
        resultJson.put("statusCode",200)
        resultJson.put("message","操作成功")

        try {
            def  alamInstance = AppBordAlarm.get(params?.id)
            alamInstance.delete(flush: true)
        }catch (Exception e){
            resultJson.put("statusCode",-200)
            resultJson.put("message","Error:${e.getMessage()}")
            e.printStackTrace()
        }finally{
            render resultJson.toString()
        }
    }
}
